//<![CDATA[

if (GBrowserIsCompatible()) {
    // this variable will collect the html which will eventualkly be placed in the side_bar
    var side_bar_html = "";

    // arrays to hold copies of the markers used by the side_bar
    // because the function closure trick doesnt work there
    var gmarkers = [];
    var i = 0;
    var comercio_class = 'odd';

    // A function to create the marker and set up the event window
    function createMarker(point,name,html,is_bookable) {
        var icon = is_bookable == 1 ? gicons["green"] : gicons["red"];
        var marker = new GMarker(point, icon);
        GEvent.addListener(marker, "click", function() {
                marker.openInfoWindowHtml(html);
                });
        // save the info we need to use later for the side_bar
        gmarkers[i] = marker;
        // add a line to the side_bar html
        comercio_class = comercio_class == 'odd' ? 'even' : 'odd';
        side_bar_html += '<li class="'+comercio_class+'"><a href="javascript:myclick(' + i + ')">' + name + '</a></li>';
        i++;
        return marker;
    }


    // This function picks up the click and opens the corresponding info window
    function myclick(i) {
        GEvent.trigger(gmarkers[i], "click");
    }

    // create the map
    map = new GMap2($("map"));
    map.enableScrollWheelZoom();
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng( 43.907787,-79.359741), 9);

}

else {
    alert("Sorry, the Google Maps API is not compatible with this browser");
}
// This Javascript is based on code provided by the
// Blackpool Community Church Javascript Team
// http://www.commchurch.freeserve.co.uk/   
// http://econym.googlepages.com/index.htm

//]]>


var gicons=[];
gicons["green"] = new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/ms/micons/grn-pushpin.png");
gicons["red"] = new GIcon(G_DEFAULT_ICON, "http://maps.google.com/mapfiles/ms/micons/red-pushpin.png");

function searchAddress(address) {
    var geo = new GClientGeocoder(); 
    geo.getLocations(address, function (result) {
            var p = result.Placemark[0].Point.coordinates;
            var lat=p[1];
            var lng=p[0];
            map.setCenter(new google.maps.LatLng(lat,lng), 15);
            });




}
var oldCity = null;
var oldMarkerName = null;

function getMyCity() {
    var ajaxRequest = new Ajax.Request("/markers/getMyLocation.php", {
        method: 'get',
        asynchronous: true,
        onComplete: function(response) {
            $('ciudad').value=response.responseText;
            searchThisAddress();
        }
    });
}


function searchThisAddress() {
    city = $('ciudad').value;
    cityName = $('city_'+city).text;
    markerName = $('nombre_marcador').value;
    address = $('direccion').value + ", " + cityName + ", spain";
    searchAddress(address);
    if(city != oldCity || markerName != oldMarkerName)  {
        side_bar_html = "";
        comercio_class = 'odd';
        oldCity = city;
        var request = GXmlHttp.create();
        request.open("GET", "markers/getMarkers.php?city="+city+"&name="+markerName, true);
        request.onreadystatechange = function() {
            if (request.readyState == 4) {
                var xmlDoc = GXml.parse(request.responseText);
                var markers = xmlDoc.documentElement.getElementsByTagName("marker");

                for (var i = 0; i < markers.length; i++) {
                    var lat = parseFloat(markers[i].getAttribute("lat"));
                    var lng = parseFloat(markers[i].getAttribute("lng"));
                    var point = new GLatLng(lat,lng);
                    var html = markers[i].getAttribute("html");
                    var label = markers[i].getAttribute("label");
                    var is_bookable = markers[i].getAttribute("is_bookable");

                    var marker = createMarker(point,label,html,is_bookable);
                    map.addOverlay(marker);
                }
                // put the assembled side_bar_html contents into the side_bar div
                $("side_bar").innerHTML = '<ul class="comercios">' + side_bar_html + '</ul>';
            }
        }
        request.send(null);
    }
    return false;
}
getMyCity();

